﻿// This File is part of Innovation Foundry's "QualityHelper Project"
//
// Copyright  2009 Innovation Foundry Pty Ltd
// All rights reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the
// Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA  02110-1301 USA
// or alternatively it may be downloaded from <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Windows.Browser;
using System.ComponentModel;
using System.Collections.ObjectModel;

using InnovationFoundry.QualityHelper.Utilities;

namespace InnovationFoundry.QualityHelper.Silverlight.BLL.Objects
{
    [ScriptableType]
    [DataContract]
    public class DiagramObject : INotifyPropertyChanged , IHasID
    {
        private ProcedureObject procedureRoot;

        public ProcedureObject ProcedureRoot
        {
            get { return procedureRoot; }
            set
            {
                if (this.procedureRoot != value)
                {
                    this.procedureRoot = value;
                    OnPropertyChanged("ProcedureRoot");
                }
            }
        }
        private ObservableCollection<ProcedureObject> procedures;

        public ObservableCollection<ProcedureObject> Procedures
        {
            get { return procedures; }
            set
            {
                if (this.procedures != value)
                {
                    this.procedures = value;
                    if (procedures != null)
                        procedures.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Procedures_CollectionChanged);
                    OnPropertyChanged("Procedures");
                }
            }
        }

        private ObservableCollection<ConnectionObject> connections;

        public ObservableCollection<ConnectionObject> Connections
        {
            get { return connections; }
            set
            {
                if (this.connections != value)
                {
                    this.connections = value;
                    if (connections != null)
                        connections.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Connections_CollectionChanged);
                    OnPropertyChanged("Connections");
                }
            }
        }

        private string title;

        public string Title
        {
            get { return title; }
            set
            {
                if (this.title != value)
                {
                    this.title = value;
                    OnPropertyChanged("Title");
                }
            }
        }

        private string diagramID;
        public string DiagramID
        {
            get { return diagramID; }
            set
            {
                if (this.diagramID != value)
                {
                    this.diagramID = value;
                    OnPropertyChanged("DiagramID");
                }
            }
        }

        public object ID
        {
            get { return diagramID; }
        }


        void Connections_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            OnPropertyChanged("Connections");
        }

        void Procedures_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            OnPropertyChanged("Procedures");
        }

       
        public DiagramObject() {

        }

        public DiagramObject(DiagramObject diagram) : this () {
            this.ProcedureRoot = diagram.ProcedureRoot;
            this.Procedures = diagram.Procedures;
            this.Connections = diagram.Connections;
            this.DiagramID = diagram.DiagramID;
            this.Title = diagram.Title;
        }
        public DiagramObject(ProcedureObject procedureRoot,ObservableCollection<ProcedureObject> procedures,ObservableCollection<ConnectionObject> connections) : this() {
            this.ProcedureRoot = procedureRoot;
            this.Procedures = procedures;
            this.Connections = connections;

        }

        public DiagramObject(string diagramID, string title,ProcedureObject procedureRoot, ObservableCollection<ProcedureObject> procedures, ObservableCollection<ConnectionObject> connections) : this() {
            this.ProcedureRoot = procedureRoot;
            this.Procedures = procedures;
            this.Connections = connections;
            this.DiagramID = diagramID;
            this.Title = title;
        }

        /// <summary>
        ///     Called when the value of a property changes.
        /// </summary>
        /// <param name="propertyName">The name of the property that has changed.</param>
        protected virtual void OnPropertyChanged(String propertyName)
        {
            if (String.IsNullOrEmpty(propertyName))
            {
                return;
            }
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        ///     Raised when the value of one of the properties changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
    }

}
